// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Web;
using Microsoft.IdentityModel.Claims;

namespace SL.IdentityModel.Server
{
    /// <summary>
    /// Service interface for the AuthenticationService
    /// </summary>
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class AuthenticationService
    {
        [OperationContract]
        public IdentityClaim[] SignIn()
        {
            return GetCurrentIdentity();
        }

        [OperationContract]
        public void SignOut()
        {
            TokenProcessor.SignOut();
        }

        [OperationContract]
        public IdentityClaim[] SignInWithIssuedToken( string xmlToken )
        {

            TokenProcessor tokenprocessor = new TokenProcessor();
            IClaimsPrincipal principal = tokenprocessor.Authenticate(xmlToken);
            tokenprocessor.CreateLoginSession(principal);
            return GetCurrentIdentity();
        }

        [DataContract]
        public class IdentityClaim
        {
            [DataMember]
            public string Issuer;

            [DataMember]
            public string OriginalIssuer;

            [DataMember]
            public string Type;

            [DataMember]
            public string Value;

            [DataMember]
            public string ValueType;
        }

        /// <summary>
        /// Creates an IdentityClaim collection by reading claims from server side Windows Identity Foundation claims Identity
        /// </summary>
        /// <returns></returns>
        private IdentityClaim[] GetCurrentIdentity()
        {
            IdentityClaim[] claims = new IdentityClaim[0];

            IClaimsPrincipal user = HttpContext.Current.User as IClaimsPrincipal;

            if( user.Identity != null && user.Identity.IsAuthenticated )
            {
                IClaimsIdentity identity = user.Identity as IClaimsIdentity;
                claims = new IdentityClaim[identity.Claims.Count];

                for( int i = 0; i < claims.Length; i++ )
                {
                    Claim claim = identity.Claims[i];

                    claims[i] = new IdentityClaim { 
                        Issuer = claim.Issuer,
                        OriginalIssuer = claim.OriginalIssuer,
                        Type = claim.ClaimType,
                        Value = claim.Value,
                        ValueType = claim.ValueType
                    };
                }
            }
            return claims;
        }
    }
}